/**
 * Created by yangyang on 17/2/8.
 */
var router = require('koa-router')();
var path = require('path');
var multer = require('koa-multer');
var fs = require( "fs" );

var upload = multer({ dest: path.resolve('./file/')});

router.post('/', upload.single('files'), function ( ctx, next ) {
    return new Promise(function( resolve, reject) {
        fs.rename (
            path.resolve('./file/' + ctx.req.file.filename),
            path.resolve('./file/' + ctx.req.file.filename + '.' + ctx.req.file.mimetype.split('/')[1]
        ), function (err, data) {
            if(err){
                reject();
            } else {
                resolve();
                ctx.body = {
                    code:200,
                    url:"http://47.93.199.212:3000/file/" +  ctx.req.file.filename + '.' + ctx.req.file.mimetype.split('/')[1]
                };
            }
        });
    });
});

module.exports = router;